/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * A form to create a new topic
 */
qx.Class.define("bip.authorview.forms.TopicPropertiesForm",
{
  extend : qx.ui.form.Form,
  
  include: [bip.authorview.forms.MControllerForm, qx.locale.MTranslation],
  
  construct : function()
  {
	
    this.base(arguments);
    // Add fields
    this.addGroupHeader("Topic properties");
    // Name
    var nameTextfield = new qx.ui.form.TextField();
    nameTextfield.setRequired(true);
    nameTextfield.setWidth(100);
    this.add(nameTextfield, "Name", null, "name");
    // Description
    var descTextfield = new qx.ui.form.TextField();
    descTextfield.setWidth(200);
    descTextfield.setRequired(true);
    this.add(descTextfield, "Description", null, "description");
    
    // Button
    var rmButton = bip.ui.form.PredefinedButtons.newDeleteButton();
    rmButton.addListener("execute",this.__onRemoveButtonClickHandler,this);
    this.addButton(rmButton);
    var saveButton = bip.ui.form.PredefinedButtons.newSaveButton("Update");
    saveButton.addListener("execute",this.__onSaveButtonClickHandler,this);
    this.addButton(saveButton);
    
  },
  
	/*
	 *****************************************************************************
    MEMBERS
	 *****************************************************************************
	 */

 	members :
 	{
 		__onSaveButtonClickHandler: function(evt) {
			if (this.validate()) {
				this.getController().updateModel();
				bip.Application.setState("running",this.tr("Updating topic, please wait"));
				bip.authorview.SharedServices.getInstance().getTopicService().addListenerOnce("state",function(evt) {
					if (evt.getData().getState() == "ok") {
						bip.Application.setState(evt.getData().getState(),"The topic has been updated");
					} else {
						bip.Application.setState(evt.getData().getState(),evt.getData().getModel().getMessage());
					}
				});
				bip.authorview.SharedServices.getInstance().getTopicService().postTopic(this.getController().getModel());

			} else {
				bip.Application.setState("error","Please correct the error(s) on this form");
			}
		},
		
		__onRemoveButtonClickHandler: function(evt) {
			if (this.validate()) {
				this.getController().updateModel();
				bip.Application.setState("running",this.tr("Deleting topic, please wait"));
				bip.authorview.SharedServices.getInstance().getTopicService().addListenerOnce("state",function(evt) {
					if (evt.getData().getState() == "ok") {
						bip.Application.setState(evt.getData().getState(),"The topic has been deleted");
						bip.services.Survey.getInstance().getSurveyTree();
					} else {
						bip.Application.setState(evt.getData().getState(),evt.getData().getModel().getMessage());
					}
				});
				bip.authorview.SharedServices.getInstance().getTopicService().removeTopic(this.getController().getModel());

			} else {
				bip.Application.setState("error","Please correct the error(s) on this form");
			}
		}
 		
 	},
 	
	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects();
	}

  
});